import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:im_demo/app/constant/storage_key.dart';
import 'package:im_demo/app/modules/login/controllers/countdown_controller.dart';
import 'package:im_demo/utils/logger_util.dart';

import '../../../data/chat/api/api.dart';
import '../../../data/chat/api/request/login_request.dart' as login_req;
import '../../../data/chat/api/request/login_type.dart';
import '../../../data/chat/api/response/base_response.dart';
import '../../../data/chat/api/response/login_response.dart';

class LoginController extends GetxController {
  var phoneNumberController = TextEditingController();
  var smsCodeController = TextEditingController();

  final countDownController = Get.put(CountdownController());

  final smsCode = ''.obs;
  final List<String> info = <String>[].obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void updatePhoneNumberInputText(String text) {
    _updateInputText(phoneNumberController, text);
  }

  void cleanPhoneNumberInputText() {
    _cleanInputText(phoneNumberController);
  }

  void updateSMSCodeInputText(String text) {
    // _updateInputText(smsCodeController, text);
  }

  void cleanSMSCodeInputText() {
    _cleanInputText(smsCodeController);
  }

  void _updateInputText(TextEditingController controller, String text) {
    // controller.text = text;
  }

  void _cleanInputText(TextEditingController controller) {
    controller.clear();
  }

  void getSMSCode() async {
    final String phoneNumber = phoneNumberController.text.trim();
    if (phoneNumber.isEmpty) {
      Get.snackbar('温馨提示', '请输入手机号');
      return;
    }

    EasyLoading.show(status: '正在获取', maskType: EasyLoadingMaskType.black);
    countDownController
      ..setInitialTime(60)
      ..start();

    final result = await Api().getSMSCode(phoneNumber);
    logE('getSMSCode()->result:$result');

    EasyLoading.dismiss();

    if (result.isSuccess) {
      BaseResponse? resp = result.data;
      logE(
        'getSMSCode()->resp:code:${resp?.code},message:${resp?.message},data:${resp?.data}',
      );

      EasyLoading.showToast('验证码获取成功');
      smsCode.value = '验证码：${resp?.data}';
    } else {
      EasyLoading.showToast('验证码获取失败');

      smsCode.value = '验证码获取失败：${result.error?.code},${result.error?.message}';
      countDownController.reset();
    }
  }

  void login() async {
    final String phoneNumber = phoneNumberController.text.trim();
    final String smsCode = smsCodeController.text.trim();

    if (phoneNumber.isEmpty) {
      Get.snackbar('温馨提示', '请输入手机号');
      return;
    }

    if (smsCode.isEmpty) {
      Get.snackbar('温馨提示', '请输验证码');
      return;
    }

    EasyLoading.show(status: '登录中', maskType: EasyLoadingMaskType.black);

    final login_req.LoginRequest request = login_req.LoginRequest(
      LoginType.PHONE,
      '',
      '',
      phoneNumber,
      smsCode,
      '',
    );

    final result = await Api().login(request);
    logE('login()->result:$result');

    EasyLoading.dismiss();

    if (result.isSuccess) {
      BaseResponse? resp = result.data;
      logE(
        'login()->resp:code:${resp?.code},message:${resp?.message},data:${resp?.data}',
      );

      final loginResponse = resp?.data;
      logE('login()->loginResponse:$loginResponse');

      final LoginResponse loginResponseObj = LoginResponse.fromJson(
        loginResponse,
      );
      await GetStorage().write(StorageKey.TOKEN, loginResponseObj.token);

      EasyLoading.showToast('登录成功');

      info.add('登录信息：$loginResponse');

      _getUserInfo();
    } else {
      EasyLoading.showToast('登录成功失败');

      info.add('登录成功失败：${result.error?.code},${result.error?.message}');
    }
  }

  void _getUserInfo() async {
    EasyLoading.show(status: '正在获取用户信息', maskType: EasyLoadingMaskType.black);

    final result = await Api().getUserInfo();
    logE('getUserInfo()->result:$result');
    EasyLoading.dismiss();

    if (result.isSuccess) {
      BaseResponse? resp = result.data;
      logE(
        'getUserInfo()->resp:code:${resp?.code},message:${resp?.message},data:${resp?.data}',
      );

      final getUserInfoResponse = resp?.data;
      logE('getUserInfo()->loginResponse:$getUserInfoResponse');

      EasyLoading.showToast('用户信息获取成功');

      info.add('用户信息：$getUserInfoResponse');
    } else {
      EasyLoading.showToast('用户信息获取失败');

      info.add('用户信息获取失败：${result.error?.code},${result.error?.message}');
    }
  }
}
